package at.chl.genetic.function.math.node;

import java.util.ArrayList;
import java.util.List;

import at.chl.genetic.function.math.AbstractMathFunction;
import at.chl.genetic.function.math.request.MathFunctionRequest;
import at.chl.genetic.function.math.result.MathFunctionResult;
import at.chl.genetic.node.IDecisionNode;

public class FunctionNode extends AbstractMathDecisionNode{

	private AbstractMathFunction function;
	private List<IDecisionNode> childNodes;
	
	

	public FunctionNode(AbstractMathFunction function){
		this.function=function;
		this.function.setParentNode(this);
		setHash(createHashCode("Function"+function.getName()));
	}
	
	public AbstractMathFunction getFunction() {
		return function;
	}

	public void setFunction(AbstractMathFunction function) {
		this.function = function;
	}

	@Override
	public MathFunctionResult evaluate(MathFunctionRequest request) {
		return function.doFunction(request);
	}

	@Override
	public int getChildrenCount() {
		//EXC nullpointer
		return function.getChildCount();
	}

	public List<IDecisionNode> getChildNodes() {
		return childNodes;
	}
	public void setChildNodes(List<IDecisionNode> childNodes) {
		this.childNodes = childNodes;
	}

	@Override
	public void print(String tab) {
		System.out.println(tab+function.getName());
		
		if(childNodes!=null){
			for(IDecisionNode node:childNodes){

				node.print(" "+tab);
			}
		}
		
	}

	@Override
	public String getName() {
		return function.getName();
	}

	@Override
	public IDecisionNode cloneNode() {
		
		AbstractMathFunction functionClone=(AbstractMathFunction)function.cloneFunction();
		
		IDecisionNode clone=new FunctionNode(functionClone);
		
		List<IDecisionNode> childClones=new ArrayList<IDecisionNode>();
		
		for(IDecisionNode child:childNodes){
			childClones.add(child.cloneNode());
		}
		clone.setChildNodes(childClones);
	
		
		return clone;
	}

}
